#!/bin/bash

#XXX: WARNING! Do not pass arg '-v' to ssh because this will break pipe when keepalive happens! - nevermind, it's something else!!!! broken pipe happens after a short while regardless of -v; it's due to [sender] io timeout after 60 seconds -- exiting  hmm https://bugzilla.samba.org/show_bug.cgi?id=7757#c3  so, using --no-compress as fix!
#example usage: rsyncy 127.0.0.21:/somedir localdirhere/ --rsh='ssh -l root -p 8822'
#//old example usage: rsyncy 'ssh -l root -p 8822' 127.0.0.21:/somedir localdirhere/

#the "-a" arg EXCEPT --devices --specials (aka "-D"), made explicit:
partialarchive='
--recursive --links --perms --times --group --owner
--no-devices --no-specials
--no-hard-links --no-acls --no-xattrs
'

update='--update' #prevent overwriting newer(than in $src) files on $dest 
constbadway='--no-update'

if test "$#" -lt "2" ; then
  echo "Usage: $0 srcdir destdir [more_rsync_params_here]"
  exit 1
fi

if test -n "$RSYNCY_OVERWRITE_EVEN_WHEN_NEWER_ON_DEST"; then
  echo "\$RSYNCY_OVERWRITE_EVEN_WHEN_NEWER_ON_DEST is set(${RSYNCY_OVERWRITE_EVEN_WHEN_NEWER_ON_DEST}), so: allowing newer files in dest to be overwritten by rsync"
  #XXX: rsync issue: rsync: --no-update: unknown option
  update=''
fi
#ssh="$1"
#shift
src="$1"
shift
dest="$1"
shift


set -e

tmpout="`mktemp --dry-run --tmpdir="/tmp" -t -- XXXXXXXXXX`"
cleanup() {
  rm "$tmpout" && if test "0$DEBUG" -eq "01"; then 
      echo "`tput setaf 7`Cleaned output file ${tmpout}`tput sgr0`"
      #7 is darker than normal text color, at least on urxvt
    fi
}
trap cleanup EXIT

#deal with inexistence of sudo, but only when already root! else will still fail to find sudo; FIXME: fail cleaner when sudo command isn't found!; FIXME: also when id isn't found! and fix all occurences of this if test (in ~/rsyncy/ mostly!)
if test "0" -eq "`id -u`"; then
  sudo=''
else
  sudo='sudo'
fi
#sudo --validate
#the actual rsync command:
set -x
#echo "${EXTRA_RSYNCY_PARAMS[@]}"
#echo "$EXTRA_RSYNCY_PARAMS"
#echo "${FOO[@]}"
time $sudo rsync --super --verbose --no-compress --partial --progress $partialarchive --checksum --delay-updates --no-ignore-errors --no-force --numeric-ids $update --info=SYMSAFE,DEL,NAME,SKIP,STATS  --no-munge-links  --preallocate --sparse --one-file-system  --no-prune-empty-dirs  --timeout=60 --protect-args "$@" -- "$src" "$dest" | tee "$tmpout"
#--rsh="$ssh"
set +x
#XXX: args that won't be recognized(rsync  version 3.1.1  protocol version 31): --no-update --no-copy-links --no-copy-unsafe-links --no-safe-links --no-fake-super --no-remove-source-files
#XXX: "The --contimeout option may only be used when connecting to an rsync daemon."  (so it doesn't apply in my case, with ssh, apparently)

#the following is meant to highlight(with red color) any events that might need attention, such as: some file(s) on $dest were newere and were thus not updated (unless you pass 4th param --no-update in order to DO overwrite them; aka by default they won't be)
#color='tty' #XXX: won't work when piped to less, even if less -R  (no colors due to grep stripping them, with =tty)
set +e  #or else grep will exit
color='always'
cat "$tmpout" | grep --color="$color" -e ' is newer$'
cat "$tmpout" | grep --color="$color" -e 'deleting ' #XXX: don't use beginning of line(aka ^ char) here because it won't match due to something like this inside the file: " 0 files...^Mdeleting patches/a/"    yeah! "rsync  version 3.1.1  protocol version 31"

echo "`tput setaf 2`Normal exit.`tput sgr0`"

